/* eslint-disable global-require */
import Vue from 'vue'
import axios from 'axios'
import './styles/index.scss'

import FormGenerator from './views/index/Home.vue'
import FormParser from './components/parser/Parser.vue'
import FormParsers from './components/parsers/index.vue'
if (!Vue.prototype.$axios) {
  Vue.prototype.$axios = axios
}
function initComponents(vue) {
  // eslint-disable-next-line no-constant-condition
  let register = function(tag, value){
    // console.log('value', value)
    let name = value.name || tag
    if(value.render){
      vue.component(name, value)
    }
  }
  // const files = require.context('./components/ts-components', true, /\/[^/]+\/\.vue$|\.js$|\.vue$/);
  const files = require.context('./components/ts-components', true, /\/[^/]+\/\.vue$|\.vue$/);
  const keys = files.keys() || []
  console.log('files', files)
  console.log('keys', keys)
  keys.forEach(key => {
    const tag = key.replace(/^\.\/(.*)\.\w+$/, '$1')
    // console.log('tag', tag, 'key', key)
    const value = files(key).default
    // console.log('value', value)
    register(tag, value)
  })
}

function install(vue, options) {
  initComponents(vue)
  vue.component('FormGenerator', FormGenerator)
  vue.component('FormParser', FormParser)
  vue.component('FormParsers', FormParsers)
}

export default {
  install
}
